<?php
/**
 * Description of table
 *
 * @author Henson
 */
class Table extends Base{
	
	private $tableTable = "tbl_table";
	
	private $orderTable = "tbl_order";

	public function delTableByEid($eid)
	{
		$sql = "delete from {$this->tableTable} where ordered='0' and eid=".$eid; //已生成订单的桌子 无法删除。
		$this->db->query($sql);
	}
	
	public function getTableByEid($eid)
	{
		$sql = "select * from {$this->tableTable} where eid=".$eid." order by id";
		$this->db->setQuery($sql);
		return $this->db->loadAssocList();
	}
	
	public function insertTable($insertArr)
	{
		$insertArr['insert_time'] = time();
		$insertArr['del'] = '0';
		return $this->db->insert($insertArr, $this->tableTable);
	}
	
	/**
	 * 
	 * @param type $tids
	 */
	public function reserveTable($orderArr, $tids)
	{
		global $_CFG;
		$this->db->query("begin");
		$oid = $this->db->insert($orderArr, $this->orderTable);
		if($oid == 0){
			$this->db->query("rollback");
			return false;
		}
		if($tids == '')
		{
			$_CFG['msg'] = 'F&B staff will contact you!';
			$this->db->query("commit");
			return true;
		}
		else
		{
			$tidArr = explode(',', $tids);
			$sql = "update {$this->tableTable} set ordered='1', oid=".$oid." where id in ('".  implode("','", $tidArr)."') and ordered='0' and del='0'";
			$this->db->query($sql);
			$effectRow = mysql_affected_rows();
			if($effectRow == count($tidArr))
			{
				$this->db->query("commit");
				return true;
			}
			else
			{
				$this->db->query("rollback");
				return false;
			}
		}
	}
	
	public function checkValidTable($eid, $number, $ids)
	{
		global $_CFG;
		$sql = "select count(*) from {$this->tableTable} where ordered='0' and eid=".$eid;
		if($this->db->loadResult($sql) > 0)
		{
			if($ids != "")
			{
				$tidArr = explode(',', $ids);
				$sql = "select sum(max) as total from {$this->tableTable} where id in ('".  implode("','", $tidArr)."')";
				$total = $this->db->loadResult($sql);
				if($total >= $number)
				{
					return true;
				}
				else
				{
					$_CFG['msg'] = "选择的位子小于总用餐人数";
					return false;
				}
			}
			else
			{
				return true;
			}
		}
		else 
		{
			$_CFG['msg'] = "sorry, no tables left!";
			return false;
		}
	}
	
	public function cancelTables($ids)
	{
		$table_temp_arr = explode(',', $ids);
		$sql = "update {$this->tableTable} set ordered='0', oid=0 where id in ('".  implode("','", $table_temp_arr)."')";
		$this->db->query($sql);
	}
	
	public function getTableNumByWhere($where)
	{
		$sql = "select count(*) from {$this->tableTable} {$where}";
		return $this->db->loadResult($sql);
	}
	
	public function updateToReserve($tids)
	{
		$sql = "update {$this->tableTable} set ordered='1', ordered_flg='1' where id in ('".  implode("','", $tids)."')";
		return $this->db->query($sql);
	}
	
	public function updateToRelease($tids)
	{
		$sql = "update {$this->tableTable} set ordered='0', ordered_flg='0' where id in ('".  implode("','", $tids)."')";
		return $this->db->query($sql);
	}
}
?>
